﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;

namespace Auction_Boxing_2
{


    class Boxing_Manager
    {
        #region fields

        State_Manager state_manager;

        Texture2D background;

        KeyboardState kb;

        Input_Handler[] inputs = new Input_Handler[4];

        BoxingPlayer[] players = new BoxingPlayer[4];

        List<BoxingPlayer> Players = new List<BoxingPlayer>();

        Rectangle bounds;

        public static Rectangle Battlefield;

        protected PlayerStatDisplay[] displays = new PlayerStatDisplay[4];

        string shit = "hit";
        string smoving = "moving";
        string sstopped = "stopped";
        string sattack1 = "attack1";
        string sattack2 = "attack2";
        string sattack3 = "attack3";
        string sjump = "jumping";
        string spickup = "puckup";
        string sdead = "dead";
        string scharging = "charging";
        string name = "ffsp1";
        string scasting = "casting";

        List<string> StateNames = new List<string>();

        Dictionary<string, Animation> ATextures = new Dictionary<string, Animation>();

        SpriteFont font;

        //When players use items, the item attacks manifest as objects.
        List<ItemInstance> itemInstances = new List<ItemInstance>();

        #endregion

        #region ItemNames

        string cane = "cane";

        # endregion

        #region ItemField



        List<string> ItemNames = new List<string>();


        //Item[,] Items;
        //List<BoxingItem>[] BoxingItems = { new List<BoxingItem>(), new List<BoxingItem>(), new List<BoxingItem>(), new List<BoxingItem>() };

        Dictionary<string, Texture2D> ItemTextures = new Dictionary<string, Texture2D>();

        #endregion

        public Boxing_Manager(State_Manager state_manager, Rectangle ClientBounds, Input_Handler[] inputs)
        {
            this.state_manager = state_manager;
            this.bounds = new Rectangle(0, 0, ClientBounds.Width, ClientBounds.Height);
            this.inputs = inputs;

            #region Add StateNames
            StateNames.Add(smoving);
            StateNames.Add(sstopped);
            StateNames.Add(sjump);
            StateNames.Add(sattack1);
            StateNames.Add(sattack3);
            StateNames.Add(scasting);
            StateNames.Add(shit);
            StateNames.Add(sdead);
            #endregion

            #region ItemNames
            ItemNames.Add(cane);

            #endregion

            //foreach(BoxingPlayer player in Players
        }



        public void LoadContent(ContentManager Content)
        {
            background = Content.Load<Texture2D>("Boxing/Boxing_Background_0");
            font = Content.Load<SpriteFont>("Menu/menufont");

            //itemInstances.Add(new CaneInstance(Content.Load<Texture2D>("BoxingItems/Cane_Attack"), new Vector2(100, 100), 1));

            // Temporary Item test Stuff



            

            //end Test Stuff


            foreach (string s in StateNames)
            {
                ATextures.Add(s, new Animation(Content.Load<Texture2D>("Boxing/" + name + s), .2f, true, 30));

            }




        }

        public void Activate(ContentManager Content)
        {
            /*List<Item>[] equippedItems = { new List<Item>(), new List<Item>(), new List<Item>(), new List<Item>() };
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    equippedItems[i].Add(new Cane(Content.Load<Texture2D>("BoxingItems/cane"),
                    Content.Load<Texture2D>("BoxingItems/Cane_Attack"),
                    Content.Load<Texture2D>("LoadOut/cane_icon")));
                    equippedItems[i].Add(new Bowler_Hat(Content.Load<Texture2D>("Items/bowlerhat_image"),
                        Content.Load<Texture2D>("BoxingItems/Bowler_Attack"),
                        Content.Load<Texture2D>("LoadOut/bowlerhat_icon")));
                    equippedItems[i].Add(new Revolver(Content.Load<Texture2D>("Items/revolver_image"),
                        Content.Load<Texture2D>("BoxingItems/Revolver_Attack"),
                        Content.Load<Texture2D>("LoadOut/revolver_icon")));
                    equippedItems[i].Add(new Boots(Content.Load<Texture2D>("Items/Boots_Image"),
                        Content.Load<Texture2D>("BoxingItems/gust_attack"),
                        Content.Load<Texture2D>("LoadOut/boots_icon"),
                        Content.Load<Texture2D>("Boxing/ffsp1charge"),
                        Content.Load<Texture2D>("Boxing/ffsp1jumping")));
                }

            }
            this.Items = equippedItems;*/

            Battlefield = new Rectangle(0, 140, bounds.Width, 55);

            List<BoxingPlayer> activePlayers = new List<BoxingPlayer>();

            for (int i = 0; i < 4; i++)
            {
                activePlayers.Add(new BoxingPlayer(bounds.Width * (1 - .9f), bounds.Height * (1 - i * .1f) - 100, "player" + i, i, Tools.WIDTH, Tools.HEIGHT, inputs[i]));
                activePlayers[i].OnUseItem += CreateInstance;
            }

            Players = activePlayers;
            Item[] equipment = new Item[4];
            for (int i = 0; i < 4; i++)
            {
                for(int j = 0; j < 4; j++)
                     equipment[j] = state_manager.equipment[i,j];
                
                Players[i].LoadContent(Content, ATextures, equipment);
            }

            for (int i = 0; i < 4; i++)
            {

                displays[i] = new PlayerStatDisplay(font, i + 1, Players[i], inputs[i], new Rectangle((i * bounds.Width / 4) + 1, 1, (bounds.Width / 4) - 2, bounds.Height / 4),
                    Content.Load<Texture2D>("White"), Content.Load<Texture2D>("White"));
            }
        }

        public void Initialize(ContentManager Content)
        {



        }

        public void CreateInstance(ItemInstance instance)
        {
            itemInstances.Add(instance);
            Debug.WriteLine("Item instance = " + (instance is BowlerHatInstance).ToString());
        }

        public bool Update(GameTime gameTime)
        {
            kb = Keyboard.GetState();

            Players.Sort();

            HandleCollisions();

            foreach (BoxingPlayer player in Players)
            {
                //player.handleCollision(Players);
                if (player.InternalState is StateCharging)
                {
                    player.handleCollision(Players);
                }
                player.update(gameTime);
            }

            List<ItemInstance> instancesToRemove = new List<ItemInstance>();

            foreach (ItemInstance item in itemInstances)
            {
                item.Update(gameTime);
                if (item.end)
                    instancesToRemove.Add(item);
            }

            for (int i = 0; i < instancesToRemove.Count; i++)
                itemInstances.Remove(instancesToRemove[i]);

            foreach (PlayerStatDisplay display in displays)
            {
                display.Update();
            }

            //Switch to Menu by pressing R
            if (kb.IsKeyDown(Keys.R))
                return true;
            else
                return false;
        }

        public void HandleCollisions()
        {
            foreach (ItemInstance instance in itemInstances)
            {
                if (!instance.isEffect)
                {
                    foreach (BoxingPlayer player in Players)
                    {
                        if (player.Hurtbox.Intersects(instance.hitbox) && player.playerindex != instance.playerId)
                        {
                            if (Math.Abs(player.Position.Y - instance.position.Y) <= 20 && !(player.InternalState is StateHit))
                                player.Hit(instance.item, instance.direction, (int)instance.hitbox.Y + instance.hitbox.Height / 2);
                        }
                    }
                }
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(background, bounds, Color.White);



            for (int i = 0; i < 4; i++)
            {
                displays[i].Draw(spriteBatch, font);

            }
            foreach (BoxingPlayer player in Players)
            {
                player.Draw(gameTime, spriteBatch);
                //spriteBatch.Draw(background, player.Hurtbox, Color.White * .5f);
            }

            foreach (ItemInstance item in itemInstances)
            {
                item.Draw(gameTime, spriteBatch);
                //spriteBatch.Draw(background, item.hitbox, Color.White * .5f);
            }

            string pass = "Press 'R' to skip";
            spriteBatch.DrawString(font, pass,
                new Vector2(bounds.Width - font.MeasureString(pass).X,
                    bounds.Height - font.MeasureString(pass).Y), Color.Black);

        }
    }
}
